package com.relsell.pointofsales;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.relsell.utility.AppVars;
import com.relsell.view.Map;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * 
 * Class used to Submit Data related to product.
 *
 */
public class MainActivity extends Activity implements OnItemSelectedListener,
		OnClickListener {

	Spinner sp1, sp2;
	ArrayAdapter<CharSequence> adapter, adapter2;
	Button bn, bn2, bn3;
	GPSTracker gps;
	double latitude, longitude;
	String phoneno = "";
	String SMS;
	String product, quantity;
	Activity ct = this;
	public static final String sproductType = "producttype";
	public static final String sproductQuantity = "producttype";
	public static final String sLoginHeader = "loggedinuserid";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_main);
		bn = (Button) findViewById(R.id.btnSubmit);
		bn2 = (Button) findViewById(R.id.buttonClear);
		bn3 = (Button) findViewById(R.id.buttonFindus);
		bn.setOnClickListener(this);
		bn2.setOnClickListener(this);
		bn3.setOnClickListener(this);

		sp1 = (Spinner) findViewById(R.id.spinner1);
		sp2 = (Spinner) findViewById(R.id.spinner2);
		adapter = ArrayAdapter.createFromResource(this, R.array.data_arrays,
				android.R.layout.simple_list_item_1);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp1.setAdapter(adapter);
		sp1.setOnItemSelectedListener(this);
		adapter2 = ArrayAdapter.createFromResource(this, R.array.number_arrays,
				android.R.layout.simple_list_item_1);
		sp2.setAdapter(adapter2);
		sp2.setOnItemSelectedListener(this);

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		Spinner spinner = (Spinner) arg0;
		if (spinner.getId() == R.id.spinner1) {
			sp1.setSelection(arg2);
			product = (String) sp1.getSelectedItem();
		} else if (spinner.getId() == R.id.spinner2) {

			sp2.setSelection(arg2);
			quantity = (String) sp2.getSelectedItem();
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btnSubmit:
			this.setProgressBarIndeterminateVisibility(true);
			new SendProduct().execute();

			gps = new GPSTracker(MainActivity.this);
			if (gps.canGetLocation()) {
				latitude = gps.getLatitude();
				longitude = gps.getLongitude();
			} else {
				gps.showSettingsAlert();
			}

			break;

		case R.id.buttonClear:
			Toast.makeText(getApplicationContext(), product, Toast.LENGTH_LONG)
					.show();
			Toast.makeText(getApplicationContext(), quantity, Toast.LENGTH_LONG)
					.show();
			sp1.setSelection(0);
			sp2.setSelection(0);
			break;

		case R.id.buttonFindus:
			Intent i = new Intent(MainActivity.this, Map.class);
			startActivity(i);
			break;

		}

	}

	public class SendProduct extends AsyncTask<Void, Void, Boolean> {

		String responseString;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

		}

		@Override
		protected Boolean doInBackground(Void... params) {
			try {

				HttpClient http = new DefaultHttpClient();
				String server = AppVars.getServer();
				HttpPost post = new HttpPost("http://" + server
						+ "/gcminteraction/view/start.php?controlVar=3");
				List<NameValuePair> data = new ArrayList<NameValuePair>();
				if (AppVars.isLogEnabled) {
					Log.v(AppVars.LOG_TAG, "Product =" + product);
					Log.v(AppVars.LOG_TAG, "Product =" + quantity);
				}
				data.add(new BasicNameValuePair(sproductType, product));
				data.add(new BasicNameValuePair(sproductQuantity, quantity));
				post.addHeader(sLoginHeader, AppVars.sloggedInUserId);
				post.setEntity(new UrlEncodedFormEntity(data));

				HttpResponse response = http.execute(post);

				responseString = new BasicResponseHandler()
						.handleResponse(response); // Basic handler
				if (AppVars.isLogEnabled) {
					Log.v(AppVars.LOG_TAG, "Response String =" + responseString);
				}
				// result = obj.getString("loggedIn");

				return true;
			} catch (ClientProtocolException e) {

			} catch (IOException e) {

			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean success) {

			ct.setProgressBarIndeterminateVisibility(false);
			Toast.makeText(getApplicationContext(), "Products Submitted",
					Toast.LENGTH_LONG).show();
		}
	}

}
